/**
 * 
 */
package com.wolfpire.info.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.info.dao.InformationCountInfoDao;
import com.wolfpire.info.model.InformationCountInfo;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;

/**
 * @author lihd
 *
 */

@Repository("informationCountInfoDao")
public class InformationCountInfoDaoImpl extends
		BaseHibernateDao<InformationCountInfo, Long> implements
		InformationCountInfoDao {

	@Override
	public Page<InformationCountInfo> findInformationCountInfos(
			Page<InformationCountInfo> page,
			InformationCountInfo filterInformationCountInfo) {
		Criterion[] criterions = this.createFilter(filterInformationCountInfo);
		int totalCount = this.findIntByCriteria(criterions);
		page.setTotalCount(totalCount);
		
		if (0 < totalCount) {
			Criteria c = this.createCriteria(criterions);
			c.addOrder(Order.desc("year"));
			c.addOrder(Order.desc("id"));
			c = this.setPageParameter(c, page);
			@SuppressWarnings("unchecked")
			List<InformationCountInfo> result = c.list();
			page.setDataList(result);
		}
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<InformationCountInfo> list(
			InformationCountInfo informationCountInfo) {
		Criterion[] criterions = this.createFilter(informationCountInfo);
		Criteria c = this.createCriteria(criterions);
		return c.list();
	}
	
	private Criterion[] createFilter(InformationCountInfo filterInformationCountInfo) {
		if (null == filterInformationCountInfo) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != filterInformationCountInfo.getId()) {
			criterions.add(Restrictions.eq("id", filterInformationCountInfo.getId()));
		}
		if (null != filterInformationCountInfo.getYear()) {
			criterions.add(Restrictions.eq("year", filterInformationCountInfo.getYear()));
		}
		if (null != filterInformationCountInfo.getDelFlag()) {
			criterions.add(Restrictions.eq("delFlag", filterInformationCountInfo.getDelFlag()));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}
	
}
